 global root1 "XXX\rural man.dta"
 
 global root2 "XXX\chns.dta"
 
 global root3  "XXX\Urban man.dta"
 
 global result "XXX\result"

 global predominent "marriage education work_status communist i.wave i.community i.city_code*i.wave"

  //Figure 1 // 
  use "$root1",clear
 
  keep if age>=48.25 & age<=72.25

  binscatter pension age,rd(60.25) n(50) linetype(qfit) ///
		      ytitle(" ")  title("Pension") 


 //Figure 2 //
   use "$root1",clear

  keep if age>=48.25 & age<=72.25

   foreach  i of var  smoking Qcig Pcig Ecig  {
  
    rdplot `i' age, c(60.25) ci(95)  p(4) kernel(triangular)  graph_options(title(Ln(EAC+1)(Smoker Sample)))  genvars hide 
  twoway (lfitci `i' age if age<60.25 & age>=60.25-5, level(95) ciplot(rline) lcolor(black) sort) ///
         (scatter rdplot_mean_y rdplot_mean_x if age<60.25 & age>=60.25-5, msize(vsmall) mcolor(black)) ///
         (lfitci `i' age if age<=60.25+5 & age>60.25, level(95) ciplot(rline) lcolor(black) sort) ///
         (scatter rdplot_mean_y rdplot_mean_x if age<=60.25+5 & age>60.25, msize(vsmall) mcolor(black) ///
          xline(60.25) xtitle(age) legend(off) title("`i'", size(medium)) )

 
 quietly {
gr_edit .plotregion1.plot1.style.editstyle area(linestyle(pattern(dash)))  
gr_edit .plotregion1.plot4.style.editstyle area(linestyle(pattern(dash))) 
gr_edit  .style.editstyle boxstyle(shadestyle(color(white))) 
gr_edit  plotregion1._xylines[1].style.editstyle linestyle(color(black)) editcopy
}       
          
          
   drop rdplot_id  rdplot_se_y rdplot_ci_l rdplot_ci_r rdplot_hat_y rdplot_id rdplot_N rdplot_min_bin rdplot_max_bin rdplot_mean_bin rdplot_mean_y rdplot_mean_x

  }
  
  
   //Table 1 //
 use "$root1",clear
 
 keep if age>=48.25 & age<=72.25

 gen pension_100=pension*100  

 sum pension_100  
 sum pension_100 if wave==2011
 sum pension_100 if wave==2013
 sum pension_100 if wave==2015
 sum pension_100 if wave==2018

 sum pension_100 if age>60
 sum pension_100 if age>60 & wave==2011 
 sum pension_100 if age>60 & wave==2013
 sum pension_100 if age>60 & wave==2015
 sum pension_100 if age>60 & wave==2018

 
 sum pension_yuan if age>60 & pension==1
 sum pension_yuan if wave==2011 & pension==1 
 sum pension_yuan if wave==2013 & pension==1
 sum pension_yuan if wave==2015 & pension==1
 sum pension_yuan if wave==2018 & pension==1  


   //Table 2 //
 use "$root1",clear
 
 keep if age>=55.25 & age<=65.25  //within bandwidth
 
   foreach y of var pension smoking Qcig Pcig Ecig Total_income Agricultural_income Wage_income Transfer_income  Total_food_expense  Food_expense Food_away_home Leisure_activities No_of_leisure_activities hobby Physical_examination Degree_of_depression Life_dissatisfaction Give_money_to_offspring Give_money_to_grandchildren marriage education work_status communist {	
  ttest `y',by(pension)
 }

 ttest Give_money_to_offspring,by(pension)
 ttest Transfer_income,by(pension)
 ttest communist,by(pension)
  
  
   
   //Table 3 //
  use "$root1",clear
 
global y "smoking Qcig Pcig Ecig"

  keep if age>=48.25 & age<=72.25

foreach y of var $y {	
 xi:reg `y' $predominent
 predict `y'_res1,resid
}


* Authomatic bandwidth models: 
cap erase $result/T3_localRD_pool_male.txt 
cap erase $result/T3_localRD_pool_male.xml 

local kernelfn "uni"  
foreach y of var $y {	
	foreach kvar of local kernelfn { 
         rdrobust `y'_res1 age , c(60.25)  p(1)  fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
    local observation=e(N_h_l)+e(N_h_r)
  outreg2 using $result/T3_localRD_pool_male, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}

local kernelfn "uni"  
foreach y of var $y {	
	foreach kvar of local kernelfn { 
         rdrobust `y'_res1 age ,  c(60.25)  h(3) p(1)  fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
   local observation=e(N_h_l)+e(N_h_r)
  outreg2 using $result/T3_localRD_pool_male, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}

local kernelfn "uni"  
foreach y of var $y {	
	foreach kvar of local kernelfn { 
         rdrobust `y'_res1 age , c(60.25)  h(4) p(1)  fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
   local observation=e(N_h_l)+e(N_h_r)
  outreg2 using $result/T3_localRD_pool_male, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}

local kernelfn "uni"  
foreach y of var $y {	
	foreach kvar of local kernelfn { 
        rdrobust `y'_res1 age , c(60.25)  h(5) p(1)  fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
   local observation=e(N_h_l)+e(N_h_r)
  outreg2 using $result/T3_localRD_pool_male, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 

	}
}

xmluse $result/T3_localRD_pool_male.xml, doctype(excel) sheet(Sheet1) cells(A2:FZ100) allstring missing nocompress clear 
export excel using $result/T3_localRD_pool_male.xlsx, sheet("T3_localRD", replace) 


   //Table 4 //
  use "$root1",clear

  keep if age>=55.25 & age<=65.25   //within bandwidth
  
  global y "smoking Qcig Pcig Ecig"

 *---------relative poverty
 egen poverty_line=median(Total_income)
  g poverty=1 if Total_income<poverty_line
  replace poverty=0 if Total_income>=poverty_line
 
 foreach y of var $y {	
  ttest `y',by(poverty)
 }
 
 
  *---------life expectancy

  foreach y of var $y {	
  ttest `y',by(life_exp)
 }
 
 
    //Table 5 //
  use "$root1",clear

 global y "smoking Qcig Pcig Ecig"
 
   keep if age>=48.25 & age<=72.25

 egen poverty_line=median(Total_income)
  g poverty=1 if Total_income<poverty_line
  replace poverty=0 if Total_income>=poverty_line

foreach y of var $y {	

xi:reg `y' $predominent

  predict `y'_res1,resid

}

  * Authomatic bandwidth models: 
cap erase $result/T5_localRD_pool_male.txt 
cap erase $result/T5_localRD_pool_male.xml 

 *----------------relative poverty==1

*+++Optimal bandwidth
*local kernelfn "tri epa uni"  

local kernelfn " uni"   
foreach y of var $y {	
	foreach kvar of local kernelfn { 
		 rdrobust `y'_res1 age if poverty==1, c(60.25)  p(1)  fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
         local observation=e(N_h_l)+e(N_h_r)

    outreg2 using $result/T5_localRD_pool_male, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}

local kernelfn " uni"   
foreach y of var $y {	
	foreach kvar of local kernelfn { 
     rdrobust `y'_res1 age if poverty==1  , c(60.25)  p(1) h(3) fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
    local observation=e(N_h_l)+e(N_h_r)
    outreg2 using $result/T5_localRD_pool_male, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}

local kernelfn " uni"   
foreach y of var $y{	
	foreach kvar of local kernelfn { 
     rdrobust `y'_res1 age if poverty==1 , c(60.25)  p(1) h(4) fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
     	local observation=e(N_h_l)+e(N_h_r)
    outreg2 using $result/T5_localRD_pool_male, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}

local kernelfn " uni"   
foreach y of var $y {	
	foreach kvar of local kernelfn { 
    rdrobust `y'_res1 age if poverty==1 , c(60.25)  p(1) h(5) fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
    	local observation=e(N_h_l)+e(N_h_r)

         outreg2 using $result/T5_localRD_pool_male, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 

	}
}

 *----------------relative poverty==0

local kernelfn " uni"   
foreach y of var $y {	
	foreach kvar of local kernelfn { 
            rdrobust `y'_res1 age if poverty==0 , c(60.25)  p(1)  fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
            local observation=e(N_h_l)+e(N_h_r)
         outreg2 using $result/T5_localRD_pool_male, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}

local kernelfn " uni"   
foreach y of var $y {	
	foreach kvar of local kernelfn { 
                    rdrobust `y'_res1 age if poverty==0 ,  c(60.25)  p(1) h(3) fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
        			local observation=e(N_h_l)+e(N_h_r)
outreg2 using $result/T5_localRD_pool_male, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}

local kernelfn " uni"   
foreach y of var $y {	
	foreach kvar of local kernelfn { 
                 rdrobust `y'_res1 age if poverty==0 , c(60.25)  p(1) h(4) fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
                 local observation=e(N_h_l)+e(N_h_r)
         outreg2 using $result/T5_localRD_pool_male, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}

local kernelfn " uni"   
foreach y of var $y {	
	foreach kvar of local kernelfn { 
                rdrobust `y'_res1 age if poverty==0 , c(60.25)  p(1) h(5) fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
             local observation=e(N_h_l)+e(N_h_r)
         outreg2 using $result/T5_localRD_pool_male, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}

xmluse $result/T5_localRD_pool_male.xml, doctype(excel) sheet(Sheet1) cells(A2:FZ100) allstring missing nocompress clear 
export excel using $result/T5_localRD_pool_male.xlsx, sheet("T5_localRD_pool_male", replace) 

  
  
    //Table 6 //
  use "$root1",clear

    keep if age>=48.25 & age<=72.25

 global y "smoking  Qcig Pcig Ecig"

foreach y of var $y {	
  xi:reg `y' $predominent
  predict `y'_res1,resid

}


  * Authomatic bandwidth models: 
cap erase $result/T6_localRD_pool_male.txt 
cap erase $result/T6_localRD_pool_male.xml 

 *----------------long life expectancy

local kernelfn " uni"   
foreach y of var  $y {	
	foreach kvar of local kernelfn { 
         rdrobust `y'_res1 age if life_exp==1, c(60.25)  p(1)  fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
         local observation=e(N_h_l)+e(N_h_r)
         outreg2 using $result/T6_localRD_pool_male, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 

	}
}

local kernelfn " uni"   
foreach y of var $y {	
	foreach kvar of local kernelfn { 
         rdrobust `y'_res1 age if life_exp==1 , c(60.25)  p(1) h(3) fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
          local observation=e(N_h_l)+e(N_h_r)
           outreg2 using $result/T6_localRD_pool_male, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}

local kernelfn " uni"   
foreach y of var $y {	
	foreach kvar of local kernelfn { 
         rdrobust `y'_res1 age if life_exp==1 , c(60.25)  p(1) h(4) fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
         local observation=e(N_h_l)+e(N_h_r)
           outreg2 using $result/T6_localRD_pool_male, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}

local kernelfn " uni"   
foreach y of var $y {	
	foreach kvar of local kernelfn { 
        rdrobust `y'_res1 age if life_exp==1 , c(60.25)  p(1) h(5) fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
        local observation=e(N_h_l)+e(N_h_r)
           outreg2 using $result/T6_localRD_pool_male, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}


 *----------------short life expectancy
  
local kernelfn " uni"   
foreach y of var $y {	
	foreach kvar of local kernelfn { 
         rdrobust `y'_res1 age if life_exp==0  , c(60.25)  p(1)  fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
 		 local observation=e(N_h_l)+e(N_h_r)
          outreg2 using $result/T6_localRD_pool_male, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}

local kernelfn " uni"   
foreach y of var $y {	
	foreach kvar of local kernelfn { 
         rdrobust `y'_res1 age if life_exp==0 ,  c(60.25)  p(1) h(3) fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
      	 local observation=e(N_h_l)+e(N_h_r)
     outreg2 using $result/T6_localRD_pool_male, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}

local kernelfn " uni"   
foreach y of var $y {	
	foreach kvar of local kernelfn { 
         rdrobust `y'_res1 age if life_exp==0 , c(60.25)  p(1) h(4) fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
 		 local observation=e(N_h_l)+e(N_h_r)
          outreg2 using $result/T6_localRD_pool_male, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}

local kernelfn " uni"   
foreach y of var $y {	
	foreach kvar of local kernelfn { 
         rdrobust `y'_res1 age if life_exp==0 , c(60.25)  p(1) h(5) fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
 		 local observation=e(N_h_l)+e(N_h_r)
          outreg2 using $result/T6_localRD_pool_male, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}

xmluse $result/T6_localRD_pool_male.xml, doctype(excel) sheet(Sheet1) cells(A2:FZ100) allstring missing nocompress clear 
export excel using $result/T6_localRD_pool_male.xlsx, sheet("T6_localRD_pool_male", replace) 



    //Table 7 //
 use "$root1",clear

   keep if age>=48.25 & age<=72.25

 global y "Total_income Agricultural_income   Wage_income Transfer_income Total_food_expense Food_expense Food_away_home Leisure_activities No_of_leisure_activities hobby Physical_examination Degree_of_depression Life_dissatisfaction Give_money_to_offspring Give_money_to_grandchildren "

 
*+++
foreach y of var $y {	

 xi:reg `y' $predominent
  predict `y'_res1,resid

}


* Authomatic bandwidth models: 
cap erase $result/Com_pool_male1.txt 
cap erase $result/Com_pool_male1.xml 

local kernelfn "uni" 
foreach y of var $y {	
	foreach kvar of local kernelfn { 
		 rdrobust `y'_res1 age , c(60.25)  p(1)  fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
    local observation=e(N_h_l)+e(N_h_r)
  outreg2 using $result/Com_pool_male1, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}



local kernelfn " uni" 
foreach y of var $y {	
	foreach kvar of local kernelfn { 
		 rdrobust `y'_res1 age ,  c(60.25)  h(3) p(1)  fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
   local observation=e(N_h_l)+e(N_h_r)
  outreg2 using $result/Com_pool_male1, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}

local kernelfn " uni"  
foreach y of var $y {	
	foreach kvar of local kernelfn { 
		 rdrobust `y'_res1 age , c(60.25)  h(4) p(1)  fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
   local observation=e(N_h_l)+e(N_h_r)
  outreg2 using $result/Com_pool_male1, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}


local kernelfn " uni"   
foreach y of var $y {	
	foreach kvar of local kernelfn { 
		 rdrobust `y'_res1 age , c(60.25)  h(5) p(1)  fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
   local observation=e(N_h_l)+e(N_h_r)
  outreg2 using $result/Com_pool_male1, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 

	}
}

xmluse $result/Com_pool_male1.xml, doctype(excel) sheet(Sheet1) cells(A2:FZ100) allstring missing nocompress clear 
export excel using $result/Com_pool_male1.xlsx, sheet("T3_localRD", replace) 


    //Table 8 //

  use "$root1",clear
    
  keep if age>=48.25 & age<=72.25

 global y "Total_income Agricultural_income Wage_income Transfer_income  Total_food_expense  Food_expense Food_away_home Leisure_activities  No_of_leisure_activities hobby Physical_examination Degree_of_depression Life_dissatisfaction Give_money_to_offspring Give_money_to_grandchildren "
 
  egen poverty_line=median(Total_income)
  g poverty=1 if Total_income<poverty_line
  replace poverty=0 if Total_income>=poverty_line

foreach y of var $y {	

xi:reg `y' $predominent

  predict `y'_res1,resid

}


* Authomatic bandwidth models: 
cap erase $result/Com_n_income.txt 
cap erase $result/Com_n_income.xml 


 *----------------relative poverty==1
local kernelfn "uni" 
foreach y of var $y {	
	foreach kvar of local kernelfn { 
		 rdrobust `y'_res1 age if poverty==1, c(60.25)  p(1)  fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
    local observation=e(N_h_l)+e(N_h_r)
  outreg2 using $result/Com_n_income, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}

local kernelfn " uni" 
foreach y of var $y {	
	foreach kvar of local kernelfn { 
		 rdrobust `y'_res1 age if poverty==1,  c(60.25)  h(3) p(1)  fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
   local observation=e(N_h_l)+e(N_h_r)
  outreg2 using $result/Com_n_income, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}

local kernelfn " uni"  
foreach y of var $y {	
	foreach kvar of local kernelfn { 
		 rdrobust `y'_res1 age if poverty==1, c(60.25)  h(4) p(1)  fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
   local observation=e(N_h_l)+e(N_h_r)
  outreg2 using $result/Com_n_income, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}


local kernelfn " uni"   
foreach y of var $y {	
	foreach kvar of local kernelfn { 
		 rdrobust `y'_res1 age if poverty==1, c(60.25)  h(5) p(1)  fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
   local observation=e(N_h_l)+e(N_h_r)
  outreg2 using $result/Com_n_income, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 

	}
}

 *----------------relative poverty==0

 local kernelfn "uni" 
foreach y of var $y {	
	foreach kvar of local kernelfn { 
		 rdrobust `y'_res1 age if poverty==0, c(60.25)  p(1)  fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
    local observation=e(N_h_l)+e(N_h_r)
  outreg2 using $result/Com_n_income, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}

local kernelfn " uni" 
foreach y of var $y {	
	foreach kvar of local kernelfn { 
		 rdrobust `y'_res1 age if poverty==0,  c(60.25)  h(3) p(1)  fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
   local observation=e(N_h_l)+e(N_h_r)
  outreg2 using $result/Com_n_income, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}

local kernelfn " uni"  
foreach y of var $y {	
	foreach kvar of local kernelfn { 
		 rdrobust `y'_res1 age if poverty==0, c(60.25)  h(4) p(1)  fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
   local observation=e(N_h_l)+e(N_h_r)
  outreg2 using $result/Com_n_income, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}


local kernelfn " uni"   
foreach y of var $y {	
	foreach kvar of local kernelfn { 
		 rdrobust `y'_res1 age if poverty==0, c(60.25)  h(5) p(1)  fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
   local observation=e(N_h_l)+e(N_h_r)
  outreg2 using $result/Com_n_income, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 

	}
}

 
xmluse $result/Com_n_income.xml, doctype(excel) sheet(Sheet1) cells(A2:FZ100) allstring missing nocompress clear 
export excel using $result/Com_n_income.xlsx, sheet("T3_localRD", replace) 


    //Table 9 //

  use "$root1",clear
  
    keep if age>=48.25 & age<=72.25

 global y "Total_income Agricultural_income Wage_income Transfer_income  Total_food_expense  Food_expense Food_away_home Leisure_activities  No_of_leisure_activities hobby Physical_examination Degree_of_depression Life_dissatisfaction Give_money_to_offspring Give_money_to_grandchildren "

foreach y of var $y {	

 xi:reg `y' $predominent
  predict `y'_res1,resid

}

* Authomatic bandwidth models: 
cap erase $result/Com_n_life.txt 
cap erase $result/Com_n_life.xml 


 *----------------long life expentancy
local kernelfn "uni" 
foreach y of var $y {	
	foreach kvar of local kernelfn { 
		 rdrobust `y'_res1 age if life_exp==1, c(60.25)  p(1)  fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
    local observation=e(N_h_l)+e(N_h_r)
  outreg2 using $result/Com_n_life, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}

*+++bandwidth 345
*local kernelfn "tri epa uni"  
local kernelfn " uni" 
  
foreach y of var $y {	
	foreach kvar of local kernelfn { 
		 rdrobust `y'_res1 age if life_exp==1,  c(60.25)  h(3) p(1)  fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
   local observation=e(N_h_l)+e(N_h_r)
  outreg2 using $result/Com_n_life, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}

*local kernelfn "tri epa uni"  
local kernelfn " uni"  
 
foreach y of var $y {	
	foreach kvar of local kernelfn { 
		 rdrobust `y'_res1 age if life_exp==1, c(60.25)  h(4) p(1)  fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
   local observation=e(N_h_l)+e(N_h_r)
  outreg2 using $result/Com_n_life, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}


*local kernelfn "tri epa uni"  
local kernelfn " uni"   

foreach y of var $y {	
	foreach kvar of local kernelfn { 
		 rdrobust `y'_res1 age if life_exp==1, c(60.25)  h(5) p(1)  fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
   local observation=e(N_h_l)+e(N_h_r)
  outreg2 using $result/Com_n_life, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 

	}
}

 *----------------short life expentancy
 local kernelfn "uni" 
foreach y of var $y {	
	foreach kvar of local kernelfn { 
		 rdrobust `y'_res1 age if life_exp==0, c(60.25)  p(1)  fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
    local observation=e(N_h_l)+e(N_h_r)
  outreg2 using $result/Com_n_life, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}

  
local kernelfn " uni" 
  
foreach y of var $y {	
	foreach kvar of local kernelfn { 
		 rdrobust `y'_res1 age if life_exp==0,  c(60.25)  h(3) p(1)  fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
   local observation=e(N_h_l)+e(N_h_r)
  outreg2 using $result/Com_n_life, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}

local kernelfn " uni"  
 
foreach y of var $y {	
	foreach kvar of local kernelfn { 
		 rdrobust `y'_res1 age if life_exp==0, c(60.25)  h(4) p(1)  fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
   local observation=e(N_h_l)+e(N_h_r)
  outreg2 using $result/Com_n_life, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}


local kernelfn " uni"   

foreach y of var $y {	
	foreach kvar of local kernelfn { 
		 rdrobust `y'_res1 age if life_exp==0, c(60.25)  h(5) p(1)  fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
   local observation=e(N_h_l)+e(N_h_r)
  outreg2 using $result/Com_n_life, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 

	}
}

 
xmluse $result/Com_n_life.xml, doctype(excel) sheet(Sheet1) cells(A2:FZ100) allstring missing nocompress clear 
export excel using $result/Com_n_life.xlsx, sheet("T3_localRD", replace) 



    //appendix- figure A1 //

  use "$root1",clear

    keep if age>=48.25 & age<=72.25

 global y "Total_income Agricultural_income Wage_income Transfer_income  Total_food_expense  Food_expense Food_away_home Leisure_activities No_of_leisure_activities hobby Physical_examination Degree_of_depression Life_dissatisfaction Give_money_to_offspring Give_money_to_grandchildren "


         foreach  i of var  $y  {
  
    rdplot `i' age, c(60.25) ci(95)  p(2) kernel(triangular)  graph_options(title(Ln(EAC+1)(Smoker Sample)))  genvars hide 
  twoway (lfitci `i' age if age<60.25 & age>=60.25-5, level(95) ciplot(rline) lcolor(black) sort) ///
         (scatter rdplot_mean_y rdplot_mean_x if age<60.25 & age>=60.25-5, msize(vsmall) mcolor(black)) ///
         (lfitci `i' age if age<=60.25+5 & age>60.25, level(95) ciplot(rline) lcolor(black) sort) ///
         (scatter rdplot_mean_y rdplot_mean_x if age<=60.25+5 & age>60.25, msize(vsmall) mcolor(black) ///
          xline(60.25) xtitle(age) legend(off) title("`i'", size(medium)) )

          *   ///  note(Note: polynominal fit of order 2.))		
           
 quietly {
gr_edit .plotregion1.plot1.style.editstyle area(linestyle(pattern(dash)))  
gr_edit .plotregion1.plot4.style.editstyle area(linestyle(pattern(dash))) 
gr_edit  .style.editstyle boxstyle(shadestyle(color(white))) 
gr_edit  plotregion1._xylines[1].style.editstyle linestyle(color(black)) editcopy
}           
          
  
   drop rdplot_id  rdplot_se_y rdplot_ci_l rdplot_ci_r rdplot_hat_y rdplot_id rdplot_N rdplot_min_bin rdplot_max_bin rdplot_mean_bin rdplot_mean_y rdplot_mean_x

  }


  
//appendix- figure A2 //

  use "$root1",clear

    DCdensity age ,breakpoint(60.25) generate(Xj Yj r0 fhat se_fhat) 
drop Xj Yj r0 fhat se_fhat
quietly{
gr_edit .yaxis1.title.text = {}
gr_edit .yaxis1.title.text.Arrpush  
gr_edit .xaxis1.title.text = {}
gr_edit .xaxis1.title.text.Arrpush age
gr_edit .style.editstyle boxstyle(shadestyle(color(white))) 
gr_edit  .plotregion1._xylines[1].style.editstyle linestyle(color(black)) 
gr_edit  .yaxis1.style.editstyle draw_major_grid(no) editcopy
gr_edit  .title.text = {}
gr_edit  .title.text.Arrpush Estimated Discontinuity:0.0377(0.0250)
}


 //appendix- figure A3 //
  use "$root1",clear
    
    keep if age>=48.25 & age<=72.25

  foreach  i of var  marriage education work_status communist   {
  
    rdplot `i' age, c(60.25) ci(95)  p(2) kernel(triangular)  graph_options(title(Ln(EAC+1)(Smoker Sample)))  genvars hide 
  twoway (lfitci `i' age if age<60.25 & age>=60.25-5, level(95) ciplot(rline) lcolor(black) sort) ///
         (scatter rdplot_mean_y rdplot_mean_x if age<60.25 & age>=60.25-5, msize(vsmall) mcolor(black)) ///
         (lfitci `i' age if age<=60.25+5 & age>60.25, level(95) ciplot(rline) lcolor(black) sort) ///
         (scatter rdplot_mean_y rdplot_mean_x if age<=60.25+5 & age>60.25, msize(vsmall) mcolor(black) ///
          xline(60.25) xtitle(age) legend(off) title("`i'", size(medium)) )

          *   ///  note(Note: polynominal fit of order 2.))		
           
 quietly {
gr_edit .plotregion1.plot1.style.editstyle area(linestyle(pattern(dash)))  
gr_edit .plotregion1.plot4.style.editstyle area(linestyle(pattern(dash))) 
gr_edit  .style.editstyle boxstyle(shadestyle(color(white))) 
gr_edit  plotregion1._xylines[1].style.editstyle linestyle(color(black)) editcopy
}           
          
  
   drop rdplot_id  rdplot_se_y rdplot_ci_l rdplot_ci_r rdplot_hat_y rdplot_id rdplot_N rdplot_min_bin rdplot_max_bin rdplot_mean_bin rdplot_mean_y rdplot_mean_x

  }

   
 
  //appendix- figure A4 //
 use "$root2",clear
 
   keep if age>=48.25 & age<=72.25

       foreach  i of var  hhincgross_cpi hhincpc_cpi work_status Yearsedu Ethnic     {
  
    rdplot `i' age, c(60.25) ci(95)  p(2) kernel(triangular)  graph_options(title(Ln(EAC+1)(Smoker Sample)))  genvars hide 
  twoway (lfitci `i' age if age<60.25 & age>=60.25-5, level(95) ciplot(rline) lcolor(black) sort) ///
         (scatter rdplot_mean_y rdplot_mean_x if age<60.25 & age>=60.25-5, msize(vsmall) mcolor(black)) ///
         (lfitci `i' age if age<=60.25+5 & age>60.25, level(95) ciplot(rline) lcolor(black) sort) ///
         (scatter rdplot_mean_y rdplot_mean_x if age<=60.25+5 & age>60.25, msize(vsmall) mcolor(black) ///
          xline(60.25) xtitle(age) legend(off) title("`i'", size(medium)) )

          *   ///  note(Note: polynominal fit of order 2.))		
           
 quietly {
gr_edit .plotregion1.plot1.style.editstyle area(linestyle(pattern(dash)))  
gr_edit .plotregion1.plot4.style.editstyle area(linestyle(pattern(dash))) 
gr_edit  .style.editstyle boxstyle(shadestyle(color(white))) 
gr_edit  plotregion1._xylines[1].style.editstyle linestyle(color(black)) editcopy
}           
          
  
   drop rdplot_id  rdplot_se_y rdplot_ci_l rdplot_ci_r rdplot_hat_y rdplot_id rdplot_N rdplot_min_bin rdplot_max_bin rdplot_mean_bin rdplot_mean_y rdplot_mean_x

  }
  

 
 //appendix- table A1 //
  use "$root1",clear
 
 keep if age>=55.25 & age<=65.25   //within bandwidth
 
   foreach y of var pension smoking Qcig Pcig Ecig Total_income Agricultural_income Wage_income Transfer_income  Total_food_expense  Food_expense Food_away_home Leisure_activities No_of_leisure_activities hobby Physical_examination Degree_of_depression Life_dissatisfaction Give_money_to_offspring Give_money_to_grandchildren marriage education work_status communist {	
  sum `y'
 }

   
  //appendix- table A2 //

  use "$root2",clear

  sum hhincgross_cpi hhincpc_cpi work_status Yearsedu Ethnic
 
 
   //appendix- table A3 //

  use "$root1",clear

    keep if age>=48.25 & age<=72.25

  xi:reg   marriage  education work_status  communist i.wave i.community i.city_code*i.wave
  predict marriage_res1,resid

local kernelfn " uni"   
foreach y of varlist marriage {	
	foreach kvar of local kernelfn { 
         rdrobust `y'_res1 age , c(60.25)  p(1)  fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
          local observation=e(N_h_l)+e(N_h_r)
         outreg2 using $result/T10_sensity_pool_male, excel addtext(Kernel Type, `e(kernel)') addstat(bandwidth, e(h_l)) ctitle(`y'_p) dec(2) append 
	}
}

local kernelfn " uni"   
foreach y of varlist marriage {	
	foreach kvar of local kernelfn { 
         rdrobust `y'_res1 age , c(60.25)  p(1) h(3) fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
         local observation=e(N_h_l)+e(N_h_r)
         outreg2 using $result/T10_sensity_pool_male, excel addtext(Kernel Type, `e(kernel)') addstat(bandwidth, e(h_l)) ctitle(`y'_p) dec(2) append 
	}
}

local kernelfn " uni"   
foreach y of varlist marriage {	
	foreach kvar of local kernelfn { 
         rdrobust `y'_res1 age , c(60.25)  p(1) h(4) fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
          local observation=e(N_h_l)+e(N_h_r)
         outreg2 using $result/T10_sensity_pool_male, excel addtext(Kernel Type, `e(kernel)') addstat(bandwidth, e(h_l)) ctitle(`y'_p) dec(2) append 
	}
}

local kernelfn " uni"   
foreach y of varlist marriage {	
	foreach kvar of local kernelfn { 
		 local observation=e(N_h_l)+e(N_h_r)
         rdrobust `y'_res1 age , c(60.25)  p(1) h(5) fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
          local observation=e(N_h_l)+e(N_h_r)
         outreg2 using $result/T10_sensity_pool_male, excel addtext(Kernel Type, `e(kernel)') addstat(bandwidth, e(h_l)) ctitle(`y'_p) dec(2) append 
	}
}

 *【2】Education
 
xi:reg education marriage  work_status  communist i.wave i.community  i.city_code*i.wave
  predict education_res1,resid

 
local kernelfn " uni"   
foreach y of varlist education {	
	foreach kvar of local kernelfn { 
         rdrobust `y'_res1 age , c(60.25)  p(1)  fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
         local observation=e(N_h_l)+e(N_h_r)
         outreg2 using $result/T10_sensity_pool_male, excel addtext(Kernel Type, `e(kernel)') addstat(bandwidth, e(h_l)) ctitle(`y'_p) dec(2) append 
	}
}

local kernelfn " uni"   
foreach y of varlist education {	
	foreach kvar of local kernelfn { 
         rdrobust `y'_res1 age , c(60.25)  p(1) h(3) fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
         local observation=e(N_h_l)+e(N_h_r)
         outreg2 using $result/T10_sensity_pool_male, excel addtext(Kernel Type, `e(kernel)') addstat(bandwidth, e(h_l)) ctitle(`y'_p) dec(2) append 
	}
}

local kernelfn " uni"   
foreach y of varlist education {	
	foreach kvar of local kernelfn { 
         rdrobust `y'_res1 age , c(60.25)  p(1) h(4) fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
         local observation=e(N_h_l)+e(N_h_r)
          outreg2 using $result/T10_sensity_pool_male, excel addtext(Kernel Type, `e(kernel)') addstat(bandwidth, e(h_l)) ctitle(`y'_p) dec(2) append 
	}
}

local kernelfn " uni"   
foreach y of varlist education {	
	foreach kvar of local kernelfn { 
         rdrobust `y'_res1 age , c(60.25)  p(1) h(5) fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
         local observation=e(N_h_l)+e(N_h_r)
         outreg2 using $result/T10_sensity_pool_male, excel addtext(Kernel Type, `e(kernel)') addstat(bandwidth, e(h_l)) ctitle(`y'_p) dec(2) append 
	}
}
 
 
 *【3】work_status
 
xi:reg work_status education marriage  communist i.wave i.community i.city_code*i.wave
  predict work_status_res1,resid

 
local kernelfn " uni"   
foreach y of varlist work_status {	
	foreach kvar of local kernelfn { 
         rdrobust `y'_res1 age , c(60.25)  p(1)  fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
         local observation=e(N_h_l)+e(N_h_r)
         outreg2 using $result/T10_sensity_pool_male, excel addtext(Kernel Type, `e(kernel)') addstat(bandwidth, e(h_l)) ctitle(`y'_p) dec(2) append 
	}
}

local kernelfn " uni"   
foreach y of varlist work_status {	
	foreach kvar of local kernelfn { 
         rdrobust `y'_res1 age , c(60.25)  p(1) h(3) fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
        local observation=e(N_h_l)+e(N_h_r)
         outreg2 using $result/T10_sensity_pool_male, excel addtext(Kernel Type, `e(kernel)') addstat(bandwidth, e(h_l)) ctitle(`y'_p) dec(2) append 
	}
}

local kernelfn " uni"   
 foreach y of varlist work_status {	
	foreach kvar of local kernelfn { 
         rdrobust `y'_res1 age , c(60.25)  p(1) h(4) fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
        local observation=e(N_h_l)+e(N_h_r)
         outreg2 using $result/T10_sensity_pool_male, excel addtext(Kernel Type, `e(kernel)') addstat(bandwidth, e(h_l)) ctitle(`y'_p) dec(2) append 
	}
}

local kernelfn " uni"   
foreach y of varlist work_status {	
	foreach kvar of local kernelfn { 
         rdrobust `y'_res1 age , c(60.25)  p(1) h(5) fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
         local observation=e(N_h_l)+e(N_h_r)
         outreg2 using $result/T10_sensity_pool_male, excel addtext(Kernel Type, `e(kernel)') addstat(bandwidth, e(h_l)) ctitle(`y'_p) dec(2) append 
	}
}
 
 
  
 *【4】communist

xi:reg  communist  work_status  education marriage  i.wave i.community i.city_code*i.wave
  predict communist_res1,resid
 
local kernelfn " uni"   
foreach y of varlist communist {	
	foreach kvar of local kernelfn { 
        rdrobust `y'_res1 age , c(60.25)  p(1)  fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
        local observation=e(N_h_l)+e(N_h_r)
         outreg2 using $result/T10_sensity_pool_male, excel addtext(Kernel Type, `e(kernel)') addstat(bandwidth, e(h_l)) ctitle(`y'_p) dec(2) append 
	}
}

local kernelfn " uni"   
foreach y of varlist communist {	
	foreach kvar of local kernelfn { 
        rdrobust `y'_res1 age , c(60.25)  p(1) h(3) fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
        local observation=e(N_h_l)+e(N_h_r)
         outreg2 using $result/T10_sensity_pool_male, excel addtext(Kernel Type, `e(kernel)') addstat(bandwidth, e(h_l)) ctitle(`y'_p) dec(2) append 
	}
}

local kernelfn " uni"   
foreach y of varlist communist {	
	foreach kvar of local kernelfn { 
         rdrobust `y'_res1 age , c(60.25)  p(1) h(4) fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
         local observation=e(N_h_l)+e(N_h_r)
         outreg2 using $result/T10_sensity_pool_male, excel addtext(Kernel Type, `e(kernel)') addstat(bandwidth, e(h_l)) ctitle(`y'_p) dec(2) append 
	}
}

local kernelfn " uni"   
foreach y of varlist communist {	
	foreach kvar of local kernelfn { 
        rdrobust `y'_res1 age , c(60.25)  p(1) h(5) fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
       local observation=e(N_h_l)+e(N_h_r)
         outreg2 using $result/T10_sensity_pool_male, excel addtext(Kernel Type, `e(kernel)') addstat(bandwidth, e(h_l)) ctitle(`y'_p) dec(2) append 
	}
}
 

    
  //appendix- table A4 //

  use "$root2",clear

global y "hhincgross_cpi hhincpc_cpi work_status  Yearseduc  Ethnic "

cap erase $result/T12_CHNS.txt 
cap erase $result/T12_CHNS.xml 

local kernelfn " uni"   
foreach y of var $y  {	
	foreach kvar of local kernelfn { 
         rdrobust `y' age, c(60.25)  p(1) kernel(`kvar')  vce(cluster commid) all
          local observation=e(N_h_l)+e(N_h_r)
  outreg2 using $result/T12_CHNS, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}



local kernelfn " uni"   
foreach y of var $y  {	
	foreach kvar of local kernelfn { 
         rdrobust `y' age, c(60.25)  h(3) p(1) kernel(`kvar')  vce(cluster commid) all
          local observation=e(N_h_l)+e(N_h_r)
  outreg2 using $result/T12_CHNS, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}


local kernelfn " uni"   
foreach y of var $y  {	
	foreach kvar of local kernelfn { 
         rdrobust `y' age, c(60.25)  h(4) p(1) kernel(`kvar')  vce(cluster commid) all
          local observation=e(N_h_l)+e(N_h_r)
  outreg2 using $result/T12_CHNS, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}

local kernelfn " uni"   
foreach y of var $y  {	
	foreach kvar of local kernelfn { 
         rdrobust `y' age, c(60.25)  h(5) p(1) kernel(`kvar')  vce(cluster commid) all
          local observation=e(N_h_l)+e(N_h_r)
  outreg2 using $result/T12_CHNS, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}


 xmluse $result/T12_CHNS.xml, doctype(excel) sheet(Sheet1) cells(A2:FZ100) allstring missing nocompress clear 
export excel using $result/T12_CHNS.xlsx, sheet("T3_localRD", replace) 

 
 
   //appendix- table A5//

use "$root1",clear

  keep if age>=48.25 & age<=72.25

global predominent_1 "marriage education work_status communist i.wave i.community "

global y "smoking Qcig Pcig Ecig"

cap erase $result/T10_sensity_pool_male.txt 
cap erase $result/T10_sensity_pool_male.xml 



foreach y of var $y {	
 xi:reg `y' $predominent_1
 predict `y'_res1,resid
}


local kernelfn " uni"  
foreach y of var $y {	
	foreach kvar of local kernelfn { 
		 rdrobust `y'_res1 age , c(60.25)  p(1)  fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
    local observation=e(N_h_l)+e(N_h_r)
  outreg2 using $result/T10_sensity_pool_male, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}


local kernelfn " uni" 
foreach y of var $y {	
	foreach kvar of local kernelfn { 
		 rdrobust `y'_res1 age,  c(60.25)  h(3) p(1)  fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
   local observation=e(N_h_l)+e(N_h_r)
  outreg2 using $result/T10_sensity_pool_male, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}

local kernelfn " uni"  
foreach y of var $y {	
	foreach kvar of local kernelfn { 
		 rdrobust `y'_res1 age  , c(60.25)  h(4) p(1)  fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
   local observation=e(N_h_l)+e(N_h_r)
  outreg2 using $result/T10_sensity_pool_male, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}


local kernelfn " uni"   
foreach y of var $y {	
	foreach kvar of local kernelfn { 
		 rdrobust `y'_res1 age , c(60.25)  h(5) p(1)  fuzzy(pension)  kernel(`kvar')  vce(cluster community) all
   local observation=e(N_h_l)+e(N_h_r)
  outreg2 using $result/T10_sensity_pool_male, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 

	}
}
 
xmluse $result/T10_sensity_pool_male.xml, doctype(excel) sheet(Sheet1) cells(A2:FZ100) allstring missing nocompress clear 
export excel using $result/T10_sensity_pool_male.xlsx, sheet("T5_localRD_pool_male", replace) 

 
   //appendix- table A6// 
 use  "$root1" ,clear

  global y "smoking Qcig Pcig Ecig"

  * Authomatic bandwidth models: 
cap erase $result/T11_sensity_pool_male4.txt 
cap erase $result/T11_sensity_pool_male4.xml 


foreach y of var $y {	

 xi:reg `y' $predominent
  predict `y'_res1,resid
}


  
local kernelfn "uni"  
foreach y of var $y {	
	foreach kvar of local kernelfn { 
		 rdrobust `y'_res1 age, c(52)  p(1)  kernel(`kvar')  vce(cluster community) all
    local observation=e(N_h_l)+e(N_h_r)
  outreg2 using $result/T11_sensity_pool_male4, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}


local kernelfn " uni" 
foreach y of var $y {	
	foreach kvar of local kernelfn { 
		 rdrobust `y'_res1 age,  c(52)  h(3) p(1)  kernel(`kvar')  vce(cluster community) all
   local observation=e(N_h_l)+e(N_h_r)
  outreg2 using $result/T11_sensity_pool_male4, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}

local kernelfn " uni"  
foreach y of var $y  {	
	foreach kvar of local kernelfn { 
		 rdrobust `y'_res1 age, c(52)  h(4) p(1)  kernel(`kvar')  vce(cluster community) all
   local observation=e(N_h_l)+e(N_h_r)
  outreg2 using $result/T11_sensity_pool_male4, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}

local kernelfn " uni"   
foreach y of var $y {	
	foreach kvar of local kernelfn { 
		 rdrobust `y'_res1 age, c(52)  h(5) p(1) kernel(`kvar')  vce(cluster community) all
   local observation=e(N_h_l)+e(N_h_r)
  outreg2 using $result/T11_sensity_pool_male4, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 

	}
}

 xmluse $result/T11_sensity_pool_male4.xml, doctype(excel) sheet(Sheet1) cells(A2:FZ100) allstring missing nocompress clear 
export excel using $result/T11_sensity_pool_male4.xlsx, sheet("T3_localRD", replace) 



 //appendix- table A7// 
 use  "$root1" ,clear
 
  global y "smoking Qcig Pcig Ecig"

  * Authomatic bandwidth models: 
cap erase $result/T11_sensity_pool_male4.txt 
cap erase $result/T11_sensity_pool_male4.xml 


foreach y of var $y {	

 xi:reg `y' $predominent
  predict `y'_res1,resid
}

  
local kernelfn "uni"  
foreach y of var $y {	
	foreach kvar of local kernelfn { 
		 rdrobust `y'_res1 age, c(50)  p(1)  kernel(`kvar')  vce(cluster community) all
    local observation=e(N_h_l)+e(N_h_r)
  outreg2 using $result/T11_sensity_pool_male4, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}


local kernelfn " uni" 
foreach y of var $y {	
	foreach kvar of local kernelfn { 
		 rdrobust `y'_res1 age,  c(50)  h(3) p(1)  kernel(`kvar')  vce(cluster community) all
   local observation=e(N_h_l)+e(N_h_r)
  outreg2 using $result/T11_sensity_pool_male4, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}

local kernelfn " uni"  
foreach y of var $y  {	
	foreach kvar of local kernelfn { 
		 rdrobust `y'_res1 age, c(50)  h(4) p(1)  kernel(`kvar')  vce(cluster community) all
   local observation=e(N_h_l)+e(N_h_r)
  outreg2 using $result/T11_sensity_pool_male4, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}

local kernelfn " uni"   
foreach y of var $y {	
	foreach kvar of local kernelfn { 
		 rdrobust `y'_res1 age, c(50)  h(5) p(1) kernel(`kvar')  vce(cluster community) all
   local observation=e(N_h_l)+e(N_h_r)
  outreg2 using $result/T11_sensity_pool_male4, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 

	}
}

 xmluse $result/T11_sensity_pool_male4.xml, doctype(excel) sheet(Sheet1) cells(A2:FZ100) allstring missing nocompress clear 
export excel using $result/T11_sensity_pool_male4.xlsx, sheet("T3_localRD", replace) 



   
   //appendix- table A8// 
 use "$root3",clear
 
 global y "smoking Qcig Pcig Ecig"

*+++
foreach y of var $y {	

 xi:reg `y' $predominent
  predict `y'_res1,resid
}


* Authomatic bandwidth models: 
cap erase $result/T12_localRD_pool_male.txt 
cap erase $result/T12_localRD_pool_male.xml 

local kernelfn " uni" 

foreach y of var $y {	
	foreach kvar of local kernelfn { 
         rdrobust `y'_res1 age , c(60.25)  p(1)  kernel(`kvar')  vce(cluster community) all
          local observation=e(N_h_l)+e(N_h_r)
 outreg2 using $result/T12_localRD_pool_male, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}



local kernelfn " uni" 
foreach y of var $y {	
	foreach kvar of local kernelfn { 
         rdrobust `y'_res1 age ,  c(60.25)  h(3) p(1) kernel(`kvar')  vce(cluster community) all
  		 local observation=e(N_h_l)+e(N_h_r)
outreg2 using $result/T12_localRD_pool_male, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}

*local kernelfn "tri epa uni"  
local kernelfn " uni"  
 
foreach y of var $y  {	
	foreach kvar of local kernelfn { 
         rdrobust `y'_res1 age, c(60.25)  h(4) p(1) kernel(`kvar')  vce(cluster community) all
  		 local observation=e(N_h_l)+e(N_h_r)
outreg2 using $result/T12_localRD_pool_male, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 
	}
}


local kernelfn " uni"   

foreach y of var $y {	
	foreach kvar of local kernelfn { 
        rdrobust `y'_res1 age , c(60.25)  h(5) p(1) kernel(`kvar')  vce(cluster community) all
  		local observation=e(N_h_l)+e(N_h_r)
  outreg2 using $result/T12_localRD_pool_male, excel addtext(Kernel Type, `e(kernel)' , Bandwidth Type, `e(bwselect)') addstat(bandwidth, `e(h_l)', observation, `observation') dec(2) append 

	}
}

xmluse $result/T12_localRD_pool_male.xml, doctype(excel) sheet(Sheet1) cells(A2:FZ100) allstring missing nocompress clear 
export excel using $result/T12_localRD_pool_male.xlsx, sheet("T3_localRD", replace) 


   
  //appendix- table A9// 
 use "$root1" ,clear

gen age2=age*age

  keep if age>=48.25 & age<=72.25

global predominent1 "age  marriage education work_status communist i.wave i.community  "

global predominent2 "age  age2 marriage education work_status communist i.wave i.community  "

global predominent3 "age age2 marriage education work_status communist i.wave i.community  i.pro_code#i.wave"

 
gen group6025=1 if age>=60.25 & age!=.
replace group6025=0 if age<60.25

ivtobit Qcig $predominent1 (pension =group6025)  ,ll (0)
est store m1

ivtobit Qcig $predominent2 (pension =group6025)  ,ll (0)
est store m2

ivtobit Qcig $predominent3 (pension =group6025)  ,ll (0)
est store m3

esttab m1 m2 m3 using  "C:\Users\Lenovo\Desktop\result\qcig.rtf",star(* 0.1 ** 0.05 *** 0.01) se(%9.3f) r2(%9.3f) b(%9.3f) replace

   
   
   
   
   
   
 
 